<?php

/**
 * @version 1.0
 * @created 08-Jun-2009 3:16:46 PM
 */
class ReadUser
{
	function __construct()
	{
	}



	/**
	 *
	 * @param user
	 */
	public static function getUser( User $user )
	{
		if( Cache::getInstance()->get($user->fbID) != false ){
			//error_log("getting user from cache", 0);
			$result["loginStatus"] = true;
			$result["user"] = Cache::getInstance()->get($user->fbID);
			return $result;
		}
		$results = array();
		$result["loginStatus"] = false;
		// Instantiate the AmazonSDB class
		$sdb = new AmazonSDB();
		// Store the name of the domain
		$domain = USERS_DOMAIN;
		// Create the domain
		$users_domain = $sdb->domain_metadata( $domain );
		if ($users_domain->isOK())
		{
			//create the query
			$sql = "select * from " . $domain . " where fbID = '". $user->fbID . "'";
			$response = $sdb->select( $sql );
			if ($response->isOK())
			{
				$response = json_decode( json_encode( $response ) );
			}else{
				return AwsUtils::parseError( $response );
			}
		}else{
			return AwsUtils::parseError( $users_domain );
		}
		if( is_array( $response->{'body'}->{'SelectResult'}->{'Item'} ) ){
			//user was returned
			$result["loginStatus"] = true;
			foreach( $response->{'body'}->{'SelectResult'}->{'Item'} as $userObj ){
				$user = new User();
				foreach( $userObj->{'Attribute'} as $itemObj ){
					if( $itemObj->{'Name'} == 'roles' ){
						//decode roles
						ReadUser::decodeRoles( $user, $itemObj->{'Value'} );
					}else{
						$user->{ $itemObj->{'Name'} } = $itemObj->{'Value'};
					}
				}
			}
		}else{
			$result["loginStatus"] = true;
			$user = new User();
			foreach( $response->{'body'}->{'SelectResult'}->{'Item'}->{'Attribute'} as $itemObj ){
				if( $itemObj->{'Name'} == 'roles' ){
					//decode roles
					ReadUser::decodeRoles( $user, $itemObj->{'Value'} );
				}else{
					$user->{ $itemObj->{'Name'} } = $itemObj->{'Value'};
				}
			}
		}
		$result["user"] = $user;
		Cache::getInstance()->add($user->fbID, $user, false, 86400);
		return $result;
	}
	
/**
	 * 
	 * 
	 */
	public static function getAllUsers()
	{
		if( Cache::getInstance()->get("users") != false ){
			//error_log("getting users from cache", 0);
			return Cache::getInstance()->get("users");
		}
		$results = array();
		// Instantiate the AmazonSDB class
		$sdb = new AmazonSDB();
		// Store the name of the domain
		$domain = USERS_DOMAIN;
		// Create the domain
		$users_domain = $sdb->domain_metadata( $domain );
		if ($users_domain->isOK())
		{
			//create the query
			$sql = "select * from " . $domain;
			$response = $sdb->select( $sql );
			if ($response->isOK())
			{
				$response = json_decode( json_encode( $response ) );
			}else{
				return AwsUtils::parseError( $response );
			}
		}else{
			return AwsUtils::parseError( $users_domain );
		}
		if( is_array( $response->{'body'}->{'SelectResult'}->{'Item'} ) ){
			foreach( $response->{'body'}->{'SelectResult'}->{'Item'} as $userObj ){
				$user = new User();
				foreach( $userObj->{'Attribute'} as $itemObj ){
					if( $itemObj->{'Name'} == 'roles' ){
						//decode roles
						ReadUser::decodeRoles( $user, $itemObj->{'Value'} );
					}else{
						$user->{ $itemObj->{'Name'} } = $itemObj->{'Value'};
					}
				}
				array_push( $results, $user);
			}
		}else{
			$user = new User();
			foreach( $response->{'body'}->{'SelectResult'}->{'Item'}->{'Attribute'} as $itemObj ){
				if( $itemObj->{'Name'} == 'roles' ){
					//decode roles
					ReadUser::decodeRoles( $user, $itemObj->{'Value'} );
				}else{
					$user->{ $itemObj->{'Name'} } = $itemObj->{'Value'};
				}
			}
			array_push( $results, $user);
		}
		Cache::getInstance()->add("users", $results, false, 86400);
		return $results;
	}
	
	protected static function decodeRoles( $user, $itemObj ){
		$roles = json_decode( $itemObj );
		if( is_array( $roles ) ){
			foreach( $roles as $roleObj ){
				$role = new Role();
				$role->id = $roleObj->{'id'};
				$role->name = $roleObj->{'name'};
				$role->description = $roleObj->{'description'};
				$role->keys = $roleObj->{'keys'};
				$user->roles[ $roleObj->name ] = $role;
			}
		}else{
			$role = new Role();
			$role->id = $roles->{'id'};
			$role->name = $roles->{'name'};
			$role->description = $roles->{'description'};
			$role->keys = $roles->{'keys'};
			$user->roles[ $role->name ] = $role;
		}
	}
}
?>